import { Injectable, Inject } from '@angular/core';
import {
  CanActivate,
  CanLoad,
  RouterStateSnapshot,
  Router,
  UrlTree,
  Route,
  ActivatedRouteSnapshot
} from '@angular/router';

import { AuthService } from '../services/auth.service';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class LoginGuard implements CanActivate {
  constructor(private authService: AuthService, private router: Router) {}
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    const url: string = state.url;
    return this.checkLogin(url);
  }
  checkLogin(url: string): boolean {
    if (this.authService.isLoggedIn) {
      this.router.navigate(['/user']);
      return false;
    }
    return true;
  }
}
